package com.fbd.servicepassengeruser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fbd.internalcommon.dto.ResponseResult;
import com.fbd.internalcommon.dto.PassengerUser;
import com.fbd.servicepassengeruser.mapper.PassengerUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserService {

    @Autowired
    private PassengerUserMapper passengerUserMapper;

    public ResponseResult loginOrRegister(String passengerPhone){
        System.out.println("手机号" + passengerPhone);
        LambdaQueryWrapper<PassengerUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PassengerUser::getPassengerPhone , passengerPhone);
        PassengerUser passengerUser = passengerUserMapper.selectOne(wrapper);
        if (passengerUser == null){
            passengerUser = new PassengerUser();
            passengerUser.setPassengerPhone(passengerPhone);
            LocalDateTime now = LocalDateTime.now();
            passengerUser.setGmtCreate(now);
            passengerUser.setGmtModified(now);
            passengerUserMapper.insert(passengerUser);
        }

        return ResponseResult.success();
    }

    public ResponseResult getUserByPhone(String passengerPhone) {
        LambdaQueryWrapper<PassengerUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PassengerUser::getPassengerPhone , passengerPhone);
        PassengerUser passengerUser = passengerUserMapper.selectOne(wrapper);
        if (passengerUser == null){
            return ResponseResult.fail("当前用户不存在");
        }
        return ResponseResult.success(passengerUser);
    }
}
